Conditions | 1 |
Paths | 2 |
Total Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
15 | function upgrades(state, visibility, upgradeService, data) { |
||
16 | let ct = this; |
||
17 | ct.state = state; |
||
18 | ct.data = data; |
||
19 | ct.upgradeService = upgradeService; |
||
20 | let sortFunc = upgradeService.sortFunctions(data.upgrades); |
||
21 | |||
22 | // tries to buy all the upgrades it can, starting from the cheapest |
||
23 | ct.buyAll = function (slot, player) { |
||
24 | let currency = data.elements[slot.element].main; |
||
25 | let cheapest; |
||
26 | let cheapestPrice; |
||
27 | do{ |
||
28 | cheapest = null; |
||
29 | cheapestPrice = Infinity; |
||
|
|||
30 | for(let up of ct.visibleUpgrades(slot, player)){ |
||
31 | let price = data.upgrades[up].price; |
||
32 | if(!slot.upgrades[up] && |
||
33 | price <= state.player.resources[currency].number){ |
||
34 | if(price < cheapestPrice){ |
||
35 | cheapest = up; |
||
36 | cheapestPrice = price; |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | if(cheapest){ |
||
41 | upgradeService.buyUpgrade(player, |
||
42 | slot.upgrades, |
||
43 | data.upgrades[cheapest], |
||
44 | cheapest, |
||
45 | cheapestPrice, |
||
46 | currency); |
||
47 | } |
||
48 | }while(cheapest); |
||
49 | }; |
||
50 | |||
51 | ct.buyUpgrade = function (name, slot, player) { |
||
52 | let price = data.upgrades[name].price; |
||
53 | let currency = data.elements[slot.element].main; |
||
54 | upgradeService.buyUpgrade(player, |
||
55 | slot.upgrades, |
||
56 | data.upgrades[name], |
||
57 | name, |
||
58 | price, |
||
59 | currency); |
||
60 | }; |
||
61 | |||
62 | ct.visibleUpgrades = function(slot, player) { |
||
63 | return visibility.visible(data.upgrades, isBasicUpgradeVisible, slot, sortFunc[player.options.sortIndex], player); |
||
64 | }; |
||
65 | |||
66 | function isBasicUpgradeVisible(name, slot, player) { |
||
67 | let isVisible = visibility.isUpgradeVisible(name, slot, data.upgrades[name], player); |
||
68 | return isVisible && (!player.options.hideBought || !slot.upgrades[name]); |
||
69 | } |
||
70 | |||
71 | ct.visibleGlobalUpgrades = function() { |
||
72 | return visibility.visible(data.global_upgrades, upgradeService.filterByTag('global')); |
||
73 | }; |
||
74 | } |
||
75 |